const { pool, router, resJson } = require('../connect')
const scoreSQL = require('../db/scoreSQL')

router.post('/scoreUp', function (req, res) {
  let _req = req;
  let _res = res;
  let now=new Date()
  let point = {
    user_name: _req.body.user_name,
    score: _req.body.score,
    time:_req.body.time,
    create_time:now
  }
  let _data;
  pool.getConnection((err, conn) => {
    conn.query(scoreSQL.insert, point, (err, result) => {
      if (result) {
        _data = {
          msg: '上传成功'
        }
      } else {
        _data = {
          code: -1,
          msg: '上传失败'
        }
      }
    })
    setTimeout(() => {
      resJson(_res, _data)
    }, 200);
    pool.releaseConnection(conn)
  })
})

  .get('/allScore', function (req, res) {
    let _res = res;
    let _data;
    pool.getConnection((err, conn) => {
      conn.query(scoreSQL.selectHead, (e, r) => {
        if (e) _data = {
          code: -1,
          msg: e
        }
        if (r) {
          if (r.length) {
            _data = {
              code: 1,
              msg: '查询成功',
              data: r
            }
          } else {
            _data = {
              code: -1,
              msg: '暂无记录'
            }
          }
        }
        setTimeout(() => {
          resJson(_res, _data)
        }, 200);
      })
      pool.releaseConnection(conn)
    })
  })

  .get('/myScore', function (req, res) {
    let point = {
      user_name: req.query.name,
    }
    let _res = res;
    let _data;
    pool.getConnection((err, conn) => {
      conn.query(scoreSQL.queryByName, point.username, (e, r) => {
        if (e) _data = {
          code: -1,
          msg: e
        }
        if (r) {
          if (r.length) {
            _data = {
              code: 1,
              msg: '查询成功',
              data: r
            }
          } else {
            _data = {
              code: -1,
              msg: '暂无记录'
            }
          }
        }
        setTimeout(() => {
          resJson(_res, _data)
        }, 200);
      })
      pool.releaseConnection(conn)
    })
  })
module.exports = router;